
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;


public class Host {
	private int port;
	private String hostName;
	private ObjectOutputStream objOutputStream;
	private ObjectInputStream objInputStream;
	private Hashtable<String, Vector<String>> fileChunkInformation = new Hashtable<String, Vector<String>>();
	
	
	Host(String hostName, int port){
		this.hostName=hostName;
		this.port=port;
	}
	
	public File getFile(String fileName){
		
		try{
			SocketChannel socket = SocketChannel.open(new InetSocketAddress(hostName, port));
			
			OutputStream outStream = socket.socket().getOutputStream();
			InputStream inStream = socket.socket().getInputStream();
			
			objOutputStream = new ObjectOutputStream(outStream);
			objInputStream = new ObjectInputStream(inStream);
			
			//System.out.println(s);
			System.out.println("sending file f... ");
			
			objOutputStream.writeObject("Client");
			//Send file name to server
			objOutputStream.writeObject(fileName);
			Object o = objInputStream.readUnshared();
			if(o instanceof Hashtable){
				fileChunkInformation = (Hashtable<String, Vector<String>>)o;
				System.out.println("Hashtable instance received");
			}else{
				System.out.println("Incorrect instance");
			}
			
			
			
		}catch(Exception e){
			System.err.println(e.getMessage());
		}
		return null;
	}
}

	
	
	/*public static void main(String args[]) throws UnknownHostException, IOException{
		
		if(args.length != 2){
			System.err.println("Usage: java <host> <port>");
			System.exit(0);
		}
		ObjectOutputStream objOutputStream;
		ObjectInputStream objInputStream;
		
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		try{
			Socket socket = new Socket(host,port);
			
			System.out.println(socket.getPort());
			
			OutputStream outStream = socket.getOutputStream();
			InputStream inStream = socket.getInputStream();
			
			objOutputStream = new ObjectOutputStream(outStream);
			objInputStream = new ObjectInputStream(inStream);
			
			File f = new File("abc.txt");
			String s= f.getName();
			System.out.println(s);
			System.out.println("sending file f... ");
			//Send file name to server
			objOutputStream.writeObject(f);
		}catch(Exception e){
			System.err.println(e.getMessage());
		}
		try{
			Socket socket = new Socket(host, port);
			
		}catch(Exception e){
			
		}
	}*/


